package su.camunda.operate.listener;


import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class FlwGlobalEventListener implements JavaDelegate {


    @EventListener
    public void onExecutionEvent(DelegateExecution delegateExecution) {
        log.info("=======================================");
        log.info("==> onExecutionEvent:全局流程监听器事件类型:{},流程实例Id:{}", delegateExecution.getEventName(), delegateExecution.getProcessInstanceId());
        delegateExecution.getVariables().forEach((k, v) -> {
            log.info("k:{},v:{}", k, v);
        });
        log.info("=======================================");
    }

    @EventListener
    public void onTaskEvent(DelegateTask delegateTask) {
        log.info("=======================================");
        String assignee = delegateTask.getAssignee();
        String taskId = delegateTask.getId();
        log.info("==> onTaskEvent:全局任务监听器事件类型:{},流程实例Id:{},节点负责人:{},节点名称:{},任务ID:{}", delegateTask.getEventName(), delegateTask.getProcessInstanceId(), assignee, delegateTask.getName(),taskId);
        delegateTask.getVariables().forEach((k, v) -> {
            log.info("k:{},v:{}", k, v);
        });
        log.info("=======================================");
    }


    @Override
    public void execute(DelegateExecution execution) throws Exception {
        log.info("=======================================");
        log.info("==> execute:全局流程监听器事件类型:{},流程实例Id:{}", execution.getEventName(), execution.getProcessInstanceId());
        execution.getVariables().forEach((v, k) -> {
            log.info("k:{},v:{}", k, v);
        });
        log.info("=======================================");
    }

}
